package com.college.controller.products;

import com.college.common.constant.ErrorMsg;
import com.college.common.pojo.ao.Message;
import com.college.common.pojo.vo.ResultVo;
import com.college.common.utils.UserUtils;
import com.college.products.service.MessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Date;

@RestController
@RequestMapping("/message")
@Api(tags = "闲置评价")
@Slf4j
public class MessageController {

    @Autowired
    private MessageService messageService;

    @ApiOperation(value = "发送闲置评价")
    @PostMapping("/send")
    public ResultVo sendMessage(@RequestBody Message message){
        Long shUserId = UserUtils.getHeaderUserId();
        message.setUserId(shUserId);
        message.setCreateTime(new Date());
        if(messageService.addMessage(message)){
            return ResultVo.success(message);
        }
        return ResultVo.fail(ErrorMsg.SYSTEM_ERROR);
    }

    @ApiOperation(value = "获取闲置评价")
    @GetMapping("/info")
    public ResultVo getMessage(@RequestParam Long id){
        return ResultVo.success(messageService.getMessage(id));
    }

    @ApiOperation(value = "获取当前闲置的所有评价")
    @GetMapping("/idle")
    public ResultVo getAllIdleMessage(@RequestParam Long idleId){
        return ResultVo.success(messageService.getAllIdleMessage(idleId));
    }

    @ApiOperation(value = "获取我收到的所有评价")
    @GetMapping("/my")
    public ResultVo getAllMyMessage(){
        Long shUserId = UserUtils.getHeaderUserId();
        return ResultVo.success(messageService.getAllMyMessage(shUserId));
    }

    @ApiOperation(value = "删除评价")
    @GetMapping("/delete")
    public ResultVo deleteMessage(@RequestParam Long id){
        if(messageService.deleteMessage(id)){
            return ResultVo.success();
        }
        return ResultVo.fail(ErrorMsg.SYSTEM_ERROR);
    }
}
